In [8]:
import numpy as np
import matplotlib.pylab as plt
import numpy.random as npr
%matplotlib inline
Setting:
In [41]:
from sklearn import datasets
X,Y = datasets.make_moons(100,noise=0.05)
In [42]:
plt.scatter(X[:,0],X[:,1],c=Y)
Out[42]:
In [43]:
D = X
In [44]:
S = set()
S.add(1)
S
Out[44]:
In [45]:
#S = set()
S = np.zeros((len(X),len(X)))
for i in range(len(X)):
for j in range(i):
if Y[i]==Y[j]:
#S.add((i,j))
S[i,j] = 1
In [46]:
plt.imshow(S,interpolation='none')
Out[46]:
In [46]:
In [47]:
# learning diagonal metric
def g(A,S,D):
''' S given as a matrix?'''
def d(x,y):
return np.sqrt((x-y).T.dot(A).dot(x-y))
first_comp = 0
second_comp = 0
for i in range(len(S)):
for j in range(i):
d_xy = d(D[i],D[j])
if S[i,j]==1:
first_comp += d_xy**2
second_comp += d_xy
return first_comp - np.log(second_comp)
In [48]:
def potential(A):
return g(np.diag(A),S,D)
In [61]:
As = npr.rand(1000,2)*0.5
In [54]:
%timeit potential(As[0])
In [62]:
ps = [potential(A) for A in As]
In [63]:
plt.scatter(As[:,0],As[:,1],c=ps,linewidths=0)
plt.colorbar()
Out[63]:
In [64]:
np.min(ps),np.max(ps)
Out[64]:
In [65]:
np.argmin(ps)
Out[65]:
In [66]:
As[np.argmin(ps)]
Out[66]:
In [ ]: